import Systemdemo, {
    add_delete_flag,
    delete_row_by_flag,
    fill_cell,
    getLastColumnIndexOfSheet,
    Num2ColumnChar,
    set_cell_color,
    set_cells
} from "@/components/js/systemdemo";
import Enum from "@/components/js/enum";
import {range} from "@/components/js/pandas";
import {refresh_position_of_sheet} from "@/components/js/calculate";

let app = wps.EtApplication();


export function 漏汽(row) {
    let data = [
        ["漏汽量", "主蒸汽流量", "t/h"],  // row+2
        ["", "", ""],
        ["", "高压门杆漏汽量", "t/h"],  // row+4
        ["", "高压门杆漏汽焓", "kJ/kg"],
        ["", "高压门杆一漏流量", "t/h"],
        ["", "高压门杆二漏流量", "t/h"],  // row+7
        ["", "高压门杆三漏流量", "t/h"],  // row+8
        ["", "高压门杆四漏流量", "t/h"],  // row+9
        ["", "", ""],
        ["", "高压前轴封漏汽量", "t/h"],  // row+11
        ["", "高压前轴封漏汽焓", "kJ/kg"],
        ["", "高压前轴封一漏流量", "t/h"],  // row+13
        ["", "高压前轴封二漏流量", "t/h"],
        ["", "高压前轴封三漏流量", "t/h"],
        ["", "高压前轴封四漏流量", "t/h"],
        ["", "", ""],
        ["", "高压后轴封漏汽量", "t/h"],  // row+18
        ["", "高压后轴封漏汽焓", "kJ/kg"],
        ["", "高压后轴封一漏流量", "t/h"],  // row+20
        ["", "高压后轴封二漏流量", "t/h"],
        ["", "高压后轴封三漏流量", "t/h"],
        ["", "高压后轴封四漏流量", "t/h"],
        ["", "", ""],
        ["", "中压前轴封漏汽量", "t/h"],  // row+25
        ["", "中压前轴封漏汽焓", "kJ/kg"],
        ["", "中压前轴封一漏流量", "t/h"],  // row+27
        ["", "中压前轴封二漏流量", "t/h"],
        ["", "中压前轴封三漏流量", "t/h"],
        ["", "中压前轴封四漏流量", "t/h"],
        ["", "", ""],
        ["", "中压后轴封漏汽量", "t/h"],  // row+32
        ["", "中压后轴封漏汽焓", "kJ/kg"],
        ["", "中压后轴封一漏流量", "t/h"],  // row+34
        ["", "中压后轴封二漏流量", "t/h"],
        ["", "中压后轴封三漏流量", "t/h"],
        ["", "中压后轴封四漏流量", "t/h"],
        ["", "", ""],
        ["", "低压前轴封漏汽量", "t/h"],  // row+39
        ["", "低压前轴封漏汽焓", "kJ/kg"],
        ["", "低压前轴封一漏流量", "t/h"],  // row+41
        ["", "低压前轴封二漏流量", "t/h"],
        ["", "低压前轴封三漏流量", "t/h"],
        ["", "低压前轴封四漏流量", "t/h"],  // row+44
        ["", "", ""],
        ["", "低压后轴封漏汽量", "t/h"],  // row+46
        ["", "低压后轴封漏汽焓", "kJ/kg"],
        ["", "低压后轴封一漏流量", "t/h"],  // row+48
        ["", "低压后轴封二漏流量", "t/h"],
        ["", "低压后轴封三漏流量", "t/h"],
        ["", "低压后轴封四漏流量", "t/h"],  // row+51
        ["", "", ""],
        ["", "均压箱至低压后轴封供汽流量", "t/h"]
    ];
    set_cells(row + 2, 1, data);
    set_cell_color(row + 2, 1, 4); // 设置第一个单元格背景色
    app.Goto(app.ActiveSheet.Cells.Item(row + 2, 1), true); //滚动窗口到更改内容
    let sheet = app.ActiveSheet.Name
    let cols = getLastColumnIndexOfSheet("数据整理")
    let sp = Enum.StandardPoint;
    for (let i = 4; i <= cols; i++) {
        fill_cell(row + 2, i, "para", "main_steam.F", "watch") // 该方法不会生成main_steam.F的引用，因此只能使用{row-i}的方式引用该行

        fill_cell(row + 5, i, "formula", "{leakage.高压门杆漏汽焓值}=H_PT({main_steam.P},{main_steam.T})");
        let append = ".(main_steam.F:" + sheet + "!" + Num2ColumnChar(i) + (row + 2) + ")" // 传入主蒸汽流量的引用位置
        fill_cell(row + 6, i, "para", "{leakage.高压门杆一漏}" + append, "delete");
        fill_cell(row + 7, i, "para", "{leakage.高压门杆二漏}" + append, "delete");
        fill_cell(row + 8, i, "para", "{leakage.高压门杆三漏}" + append, "delete");
        fill_cell(row + 9, i, "para", "{leakage.高压门杆四漏}" + append, "delete");
        let formula = "{leakage.高压门杆漏汽总量}=";
        if (Systemdemo.get_cell_value(row + 6, 2) !== "即将删除")
            formula = formula + "{row+2}";
        if (Systemdemo.get_cell_value(row + 7, 2) !== "即将删除")
            formula = formula + "+{row+3}";
        if (Systemdemo.get_cell_value(row + 8, 2) !== "即将删除")
            formula = formula + "+{row+4}";
        if (Systemdemo.get_cell_value(row + 9, 2) !== "即将删除")
            formula = formula + "+{row+5}";

        fill_cell(row + 4, i, "formula", formula, 0);

        fill_cell(row + 12, i, "formula", "{leakage.高压前轴封漏汽焓值}=H_PT({main_steam.P},{main_steam.T})");
        fill_cell(row + 13, i, "para", "{leakage.高压前轴封一漏}" + append, "delete");
        fill_cell(row + 14, i, "para", "{leakage.高压前轴封二漏}" + append, "delete");
        fill_cell(row + 15, i, "para", "{leakage.高压前轴封三漏}" + append, "delete");
        fill_cell(row + 16, i, "para", "{leakage.高压前轴封四漏}" + append, "delete");
        formula = "{leakage.high_front}="
        if (Systemdemo.get_cell_value(row + 13, 2) !== "即将删除") {
            formula = formula + "{row+2}"
        }
        if (Systemdemo.get_cell_value(row + 14, 2) !== "即将删除") {
            formula = formula + "+{row+3}"
        }
        if (Systemdemo.get_cell_value(row + 15, 2) !== "即将删除") {
            formula = formula + "+{row+4}"
        }
        if (Systemdemo.get_cell_value(row + 16, 2) !== "即将删除") {
            formula = formula + "+{row+5}"
        }
        fill_cell(row + 11, i, "formula", formula);

        fill_cell(row + 19, i, "formula", "{leakage.高压后轴封漏汽焓值}=H_PT({outlet_high.P},{outlet_high.T})");
        fill_cell(row + 20, i, "para", "{leakage.高压后轴封一漏}" + append, "delete");
        fill_cell(row + 21, i, "para", "{leakage.高压后轴封二漏}" + append, "delete");
        fill_cell(row + 22, i, "para", "{leakage.高压后轴封三漏}" + append, "delete");
        fill_cell(row + 23, i, "para", "{leakage.高压后轴封四漏}" + append, "delete");
        formula = "{leakage.high_end}="
        if (Systemdemo.get_cell_value(row + 20, 2) !== "即将删除") {
            formula = formula + "{row+2}"
        }
        if (Systemdemo.get_cell_value(row + 21, 2) !== "即将删除") {
            formula = formula + "+{row+3}"
        }
        if (Systemdemo.get_cell_value(row + 22, 2) !== "即将删除") {
            formula = formula + "+{row+4}"
        }
        if (Systemdemo.get_cell_value(row + 23, 2) !== "即将删除") {
            formula = formula + "+{row+5}"
        }
        fill_cell(row + 18, i, "formula", formula);

        fill_cell(row + 26, i, "formula", "{leakage.中压前轴封漏汽焓值}=H_PT({inlet_medium.P},{inlet_medium.T})");
        fill_cell(row + 27, i, "para", "{leakage.中压前轴封一漏}" + append, "delete");
        fill_cell(row + 28, i, "para", "{leakage.中压前轴封二漏}" + append, "delete");
        fill_cell(row + 29, i, "para", "{leakage.中压前轴封三漏}" + append, "delete");
        fill_cell(row + 30, i, "para", "{leakage.中压前轴封四漏}" + append, "delete");
        formula = "{leakage.mid_front}="
        if (Systemdemo.get_cell_value(row + 27, 2) !== "即将删除") {
            formula = formula + "{row+2}"
        }
        if (Systemdemo.get_cell_value(row + 28, 2) !== "即将删除") {
            formula = formula + "+{row+3}"
        }
        if (Systemdemo.get_cell_value(row + 29, 2) !== "即将删除") {
            formula = formula + "+{row+4}"
        }
        if (Systemdemo.get_cell_value(row + 30, 2) !== "即将删除") {
            formula = formula + "+{row+5}"
        }
        if (formula === "{leakage.mid_front}=") {  // 说明没有中压前轴封，可能是高中压合缸机组
            add_delete_flag(row + 25)
            add_delete_flag(row + 26)
            Enum.StandardPoint.leakage.href['H_mid_front'] = null
        } else {
            fill_cell(row + 25, i, "formula", formula);
        }

        fill_cell(row + 33, i, "formula", "{leakage.中压后轴封漏汽焓值}=H_PT({outlet_medium.P},{outlet_medium.T})");
        fill_cell(row + 34, i, "para", "{leakage.中压后轴封一漏}" + append, "delete");
        fill_cell(row + 35, i, "para", "{leakage.中压后轴封二漏}" + append, "delete");
        fill_cell(row + 36, i, "para", "{leakage.中压后轴封三漏}" + append, "delete");
        fill_cell(row + 37, i, "para", "{leakage.中压后轴封四漏}" + append, "delete");
        formula = "{leakage.中压后轴封漏汽量}="
        if (Systemdemo.get_cell_value(row + 34, 2) !== "即将删除") {
            formula = formula + "{row+2}"
        }
        if (Systemdemo.get_cell_value(row + 35, 2) !== "即将删除") {
            formula = formula + "+{row+3}"
        }
        if (Systemdemo.get_cell_value(row + 36, 2) !== "即将删除") {
            formula = formula + "+{row+4}"
        }
        if (Systemdemo.get_cell_value(row + 37, 2) !== "即将删除") {
            formula = formula + "+{row+5}"
        }
        fill_cell(row + 32, i, "formula", formula);

        // ----------------------------------------- 低压轴封+37 ---------------------------------------
        fill_cell(row + 40, i, "formula", "{leakage.低压前轴封漏汽焓值}=H_PT({inlet_low.P},{inlet_low.T})");
        fill_cell(row + 41, i, "para", "{leakage.低压前轴封一漏}" + append, "delete");
        fill_cell(row + 42, i, "para", "{leakage.低压前轴封二漏}" + append, "delete");
        fill_cell(row + 43, i, "para", "{leakage.低压前轴封三漏}" + append, "delete");
        fill_cell(row + 44, i, "para", "{leakage.低压前轴封四漏}" + append, "delete");
        formula = "{leakage.低压前轴封漏汽量}="
        if (Systemdemo.get_cell_value(row + 41, 2) !== "即将删除") {
            formula = formula + "{row+2}"
        }
        if (Systemdemo.get_cell_value(row + 42, 2) !== "即将删除") {
            formula = formula + "+{row+3}"
        }
        if (Systemdemo.get_cell_value(row + 43, 2) !== "即将删除") {
            formula = formula + "+{row+4}"
        }
        if (Systemdemo.get_cell_value(row + 44, 2) !== "即将删除") {
            formula = formula + "+{row+5}"
        }
        if (formula === "{leakage.低压前轴封漏汽量}=") {  // 说明没有中压前轴封，可能是高中压合缸机组
            add_delete_flag(row + 39)
            add_delete_flag(row + 40)
            Enum.StandardPoint.leakage.href['低压前轴封漏汽焓值'] = null
        } else {
            fill_cell(row + 39, i, "formula", formula);
        }

        fill_cell(row + 47, i, "formula", "{leakage.低压后轴封漏汽焓值}=H_PT({outlet_medium.P},{outlet_medium.T})");
        fill_cell(row + 48, i, "para", "{leakage.低压后轴封一漏}" + append, "delete");
        fill_cell(row + 49, i, "para", "{leakage.低压后轴封二漏}" + append, "delete");
        fill_cell(row + 50, i, "para", "{leakage.低压后轴封三漏}" + append, "delete");
        fill_cell(row + 51, i, "para", "{leakage.低压后轴封四漏}" + append, "delete");
        formula = "{leakage.低压后轴封漏汽量}="
        if (Systemdemo.get_cell_value(row + 48, 2) !== "即将删除") {
            formula = formula + "{row+2}"
        }
        if (Systemdemo.get_cell_value(row + 49, 2) !== "即将删除") {
            formula = formula + "+{row+3}"
        }
        if (Systemdemo.get_cell_value(row + 50, 2) !== "即将删除") {
            formula = formula + "+{row+4}"
        }
        if (Systemdemo.get_cell_value(row + 51, 2) !== "即将删除") {
            formula = formula + "+{row+5}"
        }
        if (formula === "{leakage.低压后轴封漏汽量}=") {  // 说明没有中压前轴封，可能是高中压合缸机组
            add_delete_flag(row + 46)
            add_delete_flag(row + 47)
            Enum.StandardPoint.leakage.href['低压前轴封漏汽焓值'] = null
        } else {
            fill_cell(row + 46, i, "formula", formula);
        }
        let leakage = sp.leakage.obj;
        let SSR2LP = leakage.get_pos_by_name("均压箱"); //均压箱至低压轴封供汽
        if (SSR2LP) {
            let F_main_steam = leakage.get_pos_by_name("主蒸汽流量")
            let formula = `{leakage.F_SSR2LP}=TREND(${SSR2LP},${F_main_steam},{row-51})`
            fill_cell(row + 53, i, "formula", formula) // 均压箱至低压缸后轴封供汽
        }
    }
    delete_row_by_flag(range(row, row + 52, 1))
    // 因为fill_cell的flag="para"时，不会也不能自动设置当前参数的行，如果在上述循环中设置，会引起单元格自己引用自己
    // 此处对相关参数单独进行设置
    refresh_position_of_sheet(sheet, row)
}